﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Lswweb.Domain;
using Lswweb.Model;


namespace Lswweb.Controls
{
    public class LinkSearch: SkinnedWebControl
    {
        private TextBox _tbxSearchKey;
        private TreeCallBackDropDownList _ddlCategory;
        private Button _btnConfirm;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (Page.IsPostBack)
                return;

            EnsureChildControls();

            if (_tbxSearchKey != null)
                _tbxSearchKey.Text = SiteContext.Current.SearchKey;

            if (_ddlCategory != null)
                _ddlCategory.SelectValue = SiteContext.Current.CategoryId;
        }

        protected override void InitializeSkin(System.Web.UI.Control skin)
        {
            _tbxSearchKey = (TextBox)skin.FindControl("tbxSearchKey");
            _ddlCategory = (TreeCallBackDropDownList)skin.FindControl("ddlCategory");
            _btnConfirm = (Button)skin.FindControl("btnConfirm");

            _ddlCategory.BaseNodeId = SiteContext.Current.BaseNodeId;

            if (_btnConfirm != null)
                _btnConfirm.Click += new EventHandler(_btnConfirm_Click);  
        }

        void _btnConfirm_Click(object sender, EventArgs e)
        {
            string url = SiteUrls.Urls.LinkAminSearch(CategoryId, KeyWord);
            SiteContext.Redirect(url);
        }

        private string KeyWord
        {
            get
            {
                string keyWord = string.Empty;
                if (_tbxSearchKey != null && !string.IsNullOrEmpty(_tbxSearchKey.Text.Trim()))
                    keyWord = _tbxSearchKey.Text.Trim();
                return keyWord;
            }
        }

        private int CategoryId
        {
            get
            {
                return _ddlCategory != null ? _ddlCategory.SelectValue : SiteContext.Current.CategoryId;
            }
        }

        protected override string DefaultSkinFilename
        {
            get { return "Link/LinkSearch.ascx"; }
        }
    }
}
